#! /bin/bash
#
# script to insert the modules for the nudaq card for various OS levels
# and to create device file entries if necessary
#
# 6.11.2006,  Christian Kurtsiefer, phyck@nus.edu.sg
#
# standard location: /etc/init.d/
# source path form modules may have to be adapted
#

# . /etc/rc.status
osversion=$(uname -r  |sed  "s/\([0-9].[0-9]\).*/\\1/")

# new thing, gets replaced by a makefile
iodriverpath=DRIVERPATH/$osversion

dev0="dt330basic"
dev1="dt330advanced"
dev1old="dt330one"
dev0old="dt330zero"
group="users"

module="dt330"
if test $osversion == "2.4";
    then driver=$module".o"; 
else driver=$module".ko"
fi

return=$rc_done

case "$1" in
     start)
	echo "inserting data translation dt33x card driver module ($driver)..."
	insmod $iodriverpath/$driver || return=$rc_failed
	if test $return == $rc_done;
	    then
# create directories
	    mkdir -p /dev/ioboards
	    
# remove stale device files
	    rm -f /dev/ioboards/dt330*
	    
	    major=$(awk "\$2~\"$module\"  {print \$1}" /proc/devices)
	    echo "dt340 driver claims following major devices:"
	    echo $major
	    cardindex=0 
	    for elm in $major
	      do echo "creating devices ${dev0}_$cardindex / ${dev1}_$cardindex  for major $elm"
# create  file entries
	      mknod /dev/ioboards/${dev0}_${cardindex} c $elm 0
	      mknod /dev/ioboards/${dev1}_${cardindex} c $elm 1
	      cardindex=$(($cardindex+1))
	    done
	    echo "make compatibility link from /dev/ioboards/${dev0}_0 to /dev/ioboards/${dev0old}"
	    ln -s /dev/ioboards/${dev0}_0 /dev/ioboards/${dev0old}
	    echo "make compatibility link from /dev/ioboards/${dev1}_0 to /dev/ioboards/${dev1old}"
	    ln -s /dev/ioboards/${dev1}_0 /dev/ioboards/${dev1old}


# set permission and group (should be more secure..)
	    chgrp $group /dev/ioboards/${dev0}*
	    chmod 660 /dev/ioboards/${dev0}*
	    chgrp $group /dev/ioboards/${dev1}*
	    chmod 660 /dev/ioboards/${dev1}*
	fi;
	echo -e "$return"
	;;
    stop)
	echo "removing nudaq dt330 module..."
	rmmod $module || return=$rc_failed
	rm -f /dev/ioboards/${dev0old}
	rm -f /dev/ioboards/${dev0}_*
	rm -f /dev/ioboards/${dev1old}
	rm -f /dev/ioboards/${dev1}_*
	echo -e "$return"
	;;
    restart|reload)
	$0 stop && $0 start || return=$rc_failed
	;;
    *)
        echo "Usage: $0 {start|stop}"
	exit 1
esac

test "$return" = "$rc_done" || exit 1
exit 0
